<template>
  <div class="app-container">
    <el-tabs v-model="activeName" type="card" @tab-click="tabClick">
      <el-tab-pane label="区域管理" name="1"></el-tab-pane>
      <el-tab-pane label="省份管理" name="2"></el-tab-pane>
      <el-tab-pane label="城市管理" name="3"></el-tab-pane>
    </el-tabs>
    <div>
      <el-input
        v-model="listQuery.AreaName"
        size="small"
        placeholder="区域名称"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="getList(1)"
      />
      <el-input
        v-if="listQuery.TypeLevel>1"
        v-model="listQuery.ProvinceName"
        size="small"
        placeholder="省份名称"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="getList(1)"
      />
      <el-input
        v-if="listQuery.TypeLevel>2"
        v-model="listQuery.CityName"
        size="small"
        placeholder="城市名称"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="getList(1)"
      />
      <el-button
        style="margin-left:20px;"
        type="primary"
        @click="getList(1)"
        size="small"
        icon="el-icon-search"
      >查询</el-button>
      <el-button type="primary" @click="handleAdd" size="small" icon="el-icon-plus">{{buttonText}}</el-button>
    </div>
    <div>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="views"
        style="width: 100%;margin-top:30px;"
        border
      >
        <el-table-column label="区域名称">
          <template slot-scope="scope">{{ scope.row.AreaName }}</template>
        </el-table-column>
        <el-table-column label="区域代码">
          <template slot-scope="scope">{{ scope.row.AreaCode }}</template>
        </el-table-column>
        <el-table-column label="省份名称" v-if="listQuery.TypeLevel>1">
          <template slot-scope="scope">{{ scope.row.ProvinceName }}</template>
        </el-table-column>
        <el-table-column label="省份代码" v-if="listQuery.TypeLevel>1">
          <template slot-scope="scope">{{ scope.row.ProvinceCode }}</template>
        </el-table-column>
        <el-table-column label="城市名称" v-if="listQuery.TypeLevel>2">
          <template slot-scope="scope">{{ scope.row.CityName }}</template>
        </el-table-column>
        <el-table-column label="城市代码" v-if="listQuery.TypeLevel>2">
          <template slot-scope="scope">{{ scope.row.CityCode }}</template>
        </el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="scope">{{ scope.row.CreateTime | formatDateSec }}</template>
        </el-table-column>
        <el-table-column align="center" width="240" label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="handleEdit(scope.row)" icon="el-icon-edit">编辑</el-button>
            <el-button
              type="text"
              size="mini"
              @click="handleDelete(scope.row)"
              icon="el-icon-delete"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="margin-top:30px">
      <go-page
        v-model="listQuery.Page"
        :pageSize="listQuery.PageSize"
        :total="listQuery.Total"
        @pagination="getList()"
      />
    </div>

    <el-dialog
      :visible.sync="areaVisible"
      :title="isEidt?'编辑信息':'新增信息'"
      width="60%"
      top="20vh"
      :close-on-click-modal="false"
    >
      <el-form
        :model="temp"
        ref="areaDialog"
        label-position="right"
        label-width="80px"
        style="width: 90%;"
      >
        <el-row v-if="listQuery.TypeLevel===1">
          <el-col :span="24">
            <el-form-item label="区域">
              <el-input v-model="temp.AreaName" placeholder="请填写区域名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="listQuery.TypeLevel>1">
          <el-col :span="24">
            <el-form-item label="区域">
              <el-select v-model="temp.AreaCode" placeholder="请选择" @change="AreaChanage">
                <el-option
                  v-for="item in areaDatas"
                  :key="item.AreaCode"
                  :label="item.AreaName"
                  :value="item.AreaCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="listQuery.TypeLevel===2">
          <el-col :span="24">
            <el-form-item label="省份">
              <el-input v-model="temp.ProvinceName" placeholder="请填写省份名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="listQuery.TypeLevel>2">
          <el-col :span="24">
            <el-form-item label="省份">
              <el-select v-model="temp.ProvinceCode" placeholder="请选择" @change="ProvinceChange">
                <el-option
                  v-for="item in provinceDatas"
                  :key="item.ProvinceCode"
                  :label="item.ProvinceName"
                  :value="item.ProvinceCode"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="listQuery.TypeLevel===3">
          <el-col :span="24">
            <el-form-item label="城市">
              <el-input v-model="temp.CityName" placeholder="请填写城市名称" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div style="text-align:right;">
        <el-button type="danger" @click="close">关闭</el-button>
        <el-button type="primary" @click="confirmData">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
import {
  reqGetAreaCitys,
  reqAddAreaCity,
  reqUpdateAreaCity
} from "@/api/areacity";

import GoPage from "./components/gopage";

export default {
  components: {
    GoPage
  },
  data() {
    return {
      activeName: "1",
      buttonText: "添加区域",
      listQuery: {
        Page: 1,
        PageSize: 8,
        Total: 0,
        OrderBy: "AreaCode",
        IsAsc: true,
        TypeLevel: 1,
        AreaCode: -1,
        AreaName: "",
        ProvinceCode: -1,
        ProvinceName: "",
        CityCode: -1,
        CityName: ""
      },
      query: {
        Page: 1,
        PageSize: 10000,
        Total: 0,
        OrderBy: "AreaCode",
        IsAsc: true,
        TypeLevel: -1,
        AreaCode: -1,
        AreaName: "",
        ProvinceCode: -1,
        ProvinceName: "",
        CityCode: -1,
        CityName: ""
      },
      temp: {
        ID: null,
        TypeLevel: 1,
        AreaCode: 101,
        AreaName: "",
        ProvinceCode: 100001,
        ProvinceName: "",
        CityCode: 10000001,
        CityName: ""
      },
      views: [],
      allViews: [],
      areaDatas: [],
      provinceDatas: [],
      loading: true,
      isEidt: false,
      areaVisible: false
    };
  },
  computed: {
    // 计算属性，只要相关联的值没有改变，直接返回，不再进行计算，与方法区别，缓存
  },
  created() {
    // 实例创建完成后执行的代码
    this.getList(1);
  },
  methods: {
    // -----------------------------------------------初始化模块
    // list
    async getList(page1) {
      this.loading = true;
      if (page1 === 1) {
        this.listQuery.Page = 1;
      }
      let res = await reqGetAreaCitys(this.listQuery);
      if (res.code === 200) {
        this.views = res.data.Items;
        this.listQuery.Total = res.data.TotalItems;
        this.loading = false;
      }
      let res1 = await reqGetAreaCitys(this.query);
      if (res1.code === 200) {
        this.allViews = res1.data.Items;
        this.areaDatas = this.allViews.filter(s => s.TypeLevel === 1);
      }
    },
    // -----------------------------------------------搜索模块
    // tabl切换
    tabClick(tab, event) {
      this.listQuery.TypeLevel = tab.name - 0;
      switch (this.listQuery.TypeLevel) {
        case 1:
          this.buttonText = "添加区域";
          break;
        case 2:
          this.buttonText = "添加省份";
          break;
        case 3:
          this.buttonText = "添加城市";
          break;
        default:
          break;
      }
      this.listQuery.AreaName = "";
      this.listQuery.ProvinceName = "";
      this.listQuery.CityName = "";
      this.getList(1);
    },
    // 切换区域下拉
    AreaChanage() {
      let obj = this.areaDatas.find(item => {
        return item.AreaCode === this.temp.AreaCode;
      });
      this.temp.AreaName = obj.AreaName;
      this.provinceDatas = this.allViews.filter(
        s => s.TypeLevel === 2 && s.AreaCode === this.temp.AreaCode
      );
      this.temp.ProvinceCode = this.provinceDatas[0].ProvinceCode;
      this.temp.ProvinceName = this.provinceDatas[0].ProvinceName;
    },
    // 切换省份下拉
    ProvinceChange() {
      let obj = this.provinceDatas.find(item => {
        return item.ProvinceCode === this.temp.ProvinceCode;
      });
      this.temp.ProvinceName = obj.ProvinceName;
    },
    // -----------------------------------------------业务操作模块
    restTemp() {
      this.temp = {
        ID: null,
        TypeLevel: 1,
        AreaCode: 101,
        AreaName: "",
        ProvinceCode: 100001,
        ProvinceName: "",
        CityCode: 10000001,
        CityName: ""
      };
    },
    handleAdd() {
      this.isEidt = false;
      this.areaVisible = true;
      switch (this.listQuery.TypeLevel) {
        case 1:
          break;
        case 2:
          this.AreaChanage();
          break;
        case 3:
          this.AreaChanage();
          this.ProvinceChange();
          break;
        default:
          break;
      }
    },
    // 校验数据新增/更新
    confirmData() {
      switch (this.listQuery.TypeLevel) {
        case 1:
          if (!this.temp.AreaName) {
            this.$message({
              message: "请输入区域名称",
              type: "warning",
              duration: 3000
            });
            return;
          }
          break;
        case 2:
          if (!this.temp.ProvinceName) {
            this.$message({
              message: "请输入省份名称",
              type: "warning",
              duration: 3000
            });
            return;
          }
          break;
        case 3:
          if (!this.temp.CityName) {
            this.$message({
              message: "请输入城市名称",
              type: "warning",
              duration: 3000
            });
            return;
          }
          break;
        default:
          break;
      }
      if (this.isEidt) this.areaCityUpdate();
      else this.areaCityAdd();
    },
    // 新增区域信息数据
    async areaCityAdd() {
      switch (this.listQuery.TypeLevel) {
        case 1:
          this.temp.TypeLevel = 1;
          this.temp.ProvinceCode = 0;
          this.temp.ProvinceName = "";
          this.temp.CityCode = 0;
          this.temp.CityName = "";
          this.temp.ParentCode = 0;
          break;
        case 2:
          this.temp.TypeLevel = 2;
          this.temp.CityCode = 0;
          this.temp.CityName = "";
          break;
        case 3:
          this.temp.TypeLevel = 3;
          break;
        default:
          break;
      }
      let res = await reqAddAreaCity(this.temp);
      if (res.code === 200) {
        if (res.data) {
          this.$notify({
            title: "成功",
            message: "操作成功",
            type: "success",
            duration: 3000
          });
          this.areaVisible = false;
          this.getList(1);
          this.restTemp();
        } else {
          this.$notify({
            title: "失败",
            message: res.message,
            type: "error",
            duration: 3000
          });
        }
      }
    },
    // 更新区域信息数据
    handleEdit(row) {
      this.isEidt = true;
      this.areaVisible = true;
      this.temp = Object.assign({}, row);
      switch (this.listQuery.TypeLevel) {
        case 1:
          break;
        case 2:
          this.AreaChanage();
          this.temp.ProvinceCode = row.ProvinceCode;
          break;
        case 3:
          this.AreaChanage();
          this.temp.ProvinceCode = row.ProvinceCode;
          this.ProvinceChange();
          this.temp.CityCode = row.CityCode;
          break;
        default:
          break;
      }
    },
    // 更新区域信息数据
    async areaCityUpdate() {
      let res = await reqUpdateAreaCity(this.temp);
      if (res.code === 200) {
        if (res.data) {
          this.$notify({
            title: "成功",
            message: "操作成功",
            type: "success",
            duration: 3000
          });
          this.areaVisible = false;
          this.getList(1);
          this.restTemp();
        } else {
          this.$notify({
            title: "失败",
            message: res.message,
            type: "error",
            duration: 3000
          });
        }
      }
    },
    // 删除区域信息数据
    async handleDelete(row) {
      this.$confirm("确认删除此条数据?", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          row.IsDelete = true;
          let res = await reqUpdateAreaCity(row);
          if (res.code === 200) {
            if (res.data) {
              this.$notify({
                title: "成功",
                message: "操作成功",
                type: "success",
                duration: 3000
              });
            } else {
              this.$notify({
                title: "失败",
                message: res.message,
                type: "error",
                duration: 3000
              });
            }
          }
          this.getList(1);
        })
        .catch(err => {
          // console.error(err);
        });
    },
    close() {
      this.areaVisible = false;
      this.restTemp();
    }
  }
};
</script>